# -*- coding: UTF-8 -*-

import base
import re

class Tinypic(base.Core):

    __label__ = "TinyPic"
    __url__ = "www.tinypic.com"
    
    def uploadFile(self, image):
        params = {"the_file": open(image, "rb"), "file_type":'image', "action":'upload'}
        f = self.opener.open("http://s4.tinypic.com/upload.php", params)
        data = f.read()
        f.close()
        return data
        
    def response(self, data):
        pic = re.search("name=\"pic\" value=\"(.+?)\"", data).group(1)
        ival = re.search("name=\"ival\" value=\"(.+?)\"", data).group(1)
        s = re.search("name=\"s\" value=\"(.+?)\"", data).group(1)
        self.image_link = "http://i%s.tinypic.com/%s.jpg" % (ival, pic)
        self.ad_link = "http://tinypic.com/view.php?pic=%s&s=%s" % (pic, s)
        self.thumb_link = "http://i%s.tinypic.com/%s_th.jpg" % (ival, pic)
        self.image_name = pic + ".jpg"
